package testdemo.controller;

import edu.nf.beans.annotations.Component;
import edu.nf.beans.annotations.Inject;
import edu.nf.web.ActionContext;
import edu.nf.web.ViewResult;
import edu.nf.web.annotation.RequestMapping;
import edu.nf.web.view.JsonView;
import testdemo.dto.ValueDto;
import testdemo.exception.LoginException;
import testdemo.model.Users;
import testdemo.service.LoginService;
import testdemo.dto.ValueDto;
import testdemo.service.NoticeService;

import java.util.List;
import java.util.Map;

@Component
public class UserController {

    @Inject(name = "loginService")
    private LoginService service;

    @Inject(name="NoticeService")
    private NoticeService noticeService;

    @RequestMapping("/login")
    public ViewResult login(Users user){
        ValueDto dto = new ValueDto();
        try {
            user = service.login(user);
            ActionContext.getContext().getSession().put("user", user);
            dto.setValue(user);
        } catch (LoginException e) {
            dto.setCode(500);
            dto.setMessage(e.getMessage());
        }
        return new JsonView(dto);
    }

    @RequestMapping("/getAllUsers")
    public ViewResult getAllUsers(){
        ValueDto dto = new ValueDto();
        try {
            List<Map<String, Object>> allnotice = noticeService.getAllnotice();
            System.out.println(allnotice.size());
            dto.setValue(allnotice);
        } catch (LoginException e) {
            dto.setCode(500);
            dto.setMessage(e.getMessage());
        }
        return new JsonView(dto);
    }




}
